import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../utils/dio_util.dart';

class CartNum extends StatefulWidget {
  final Map<String, dynamic> itemData;

  CartNum(this.itemData, {Key key}) : super(key: key);

  _CartNumState createState() => _CartNumState();
}

class _CartNumState extends State<CartNum> {
  Map<String, dynamic> _itemData;
  int _cartNum; //购物车数量
  TextEditingController centerEdit=TextEditingController();
  Future<Map<String, dynamic>> setCartCount(int count) async {
    String api = '/api/mall/cart/count/edit.service';
    DioUtil http = DioUtil();
    Map<String, dynamic> param = {
      'cardId': _itemData['mallStoreCart']['cartId'],
      'number': count
    };
    Map<String, dynamic> res = await http.get(api, data: param);
    if (res['code'] == 200) {
      print('修改购物车数量成功！');
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      _itemData = widget.itemData;
      _cartNum = widget.itemData['mallStoreCart']['cartNum'];
    });
//    centerEdit.addListener(() {
//      centerEdit.text=_cartNum.toString();
//    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 168.w,
      decoration:
          BoxDecoration(border: Border.all(width: 2.w, color: Colors.black12)),
      child: Row(
        children: <Widget>[_leftBtn(), _centerArea(), _rightBtn()],
      ),
    );
  }

  //左侧按钮

  Widget _leftBtn() {
    return InkWell(
      onTap: () {
        if (_cartNum > 1) {
          setState(() {
            _cartNum = _cartNum - 1;
          });
          setCartCount(_cartNum);
        }
      },
      child: Container(
        alignment: Alignment.center,
        width: 45.w,
        height: 45.h,
        child: Text("-"),
      ),
    );
  }

  //右侧按钮
  Widget _rightBtn() {
    return InkWell(
      onTap: () {
        setState(() {
          _cartNum = _cartNum + 1;
        });
        setCartCount(_cartNum);
      },
      child: Container(
        alignment: Alignment.center,
        width: 45.w,
        height: 45.h,
        child: Text("+"),
      ),
    );
  }

//中间
  Widget _centerArea() {
    return InkWell(
      onTap: () {
        _cartNum++;
      },
      child: Container(
        alignment: Alignment.center,
        width: 70.w,
        decoration: BoxDecoration(
            border: Border(
          left: BorderSide(width: 2.w, color: Colors.black12),
          right: BorderSide(width: 2.w, color: Colors.black12),
        )),
        height: 45.h,
child: Text('${_cartNum}'),
//        child: TextField(
//            decoration: InputDecoration(
//              border: InputBorder.none,
//            ),
//          textAlign: TextAlign.center,
//          keyboardType: TextInputType.number,
//          controller: centerEdit,
//          onChanged: (value){
//            setState(() {
//              _cartNum=int.parse(value);
//            });
//          },
//        ),
      ),
    );
  }
}
